package process;

import iblink.adapter.IBFacade;
import iblink.adapter.IBFacadeFactory;
import iblink.core.Stock;

import java.util.List;

import dal.IHistoricalDataAccessor;

import packutil.FileUtil;
import packutil.exception.InvalidArgumentException;

public class LoadNewSymbolsProcess extends AProcess {

	private IHistoricalDataAccessor histDataSource;

	private IBFacade ibFacade;

	protected LoadNewSymbolsProcess() {}
	
	public LoadNewSymbolsProcess(IHistoricalDataAccessor histDataSource,
			IBFacadeFactory ibFacadeFactory) {
		this.ibFacade = ibFacadeFactory.createIBFacade();
		this.histDataSource = histDataSource;
	}

	@Override
	public void doProcess(String args[]) {

		if (args == null || args.length != 1)
			throw new InvalidArgumentException(
					"Requires one argument: path to file of symbols to load");

		List<String> symbols = FileUtil.readStringsFromFile(args[0]);

		// TODO: make sure that the stock's primary exchange is in the US

		for (String s : symbols) {
			Stock stk = ibFacade.getStock(s);
			histDataSource.addUpdateStock(stk);
		}
	}
}